//
//  MainViewController.swift
//  DSWeiBo
//
//  Created by iOS聚商 on 2017/6/27.
//  Copyright © 2017年 dreamSun. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        
//        //设置当前控制器对应tabbar颜色
//        tabBar.tintColor = UIColor.orange
       //添加子控制器
        addChildViewControllers()
        
        // Do any additional setup after loading the view.
 
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        setComposeBtn()
    }
    
    
    
    private func setComposeBtn()
    {
        //添加“+”按钮
        tabBar.addSubview(composBtn)
        
        let width = UIScreen.main.bounds.size.width/CGFloat(viewControllers!.count)
        let rect = CGRect(x: 0, y: 0, width: width, height: 49)
        composBtn.frame = rect.offsetBy(dx: 2*width, dy: 0)
        
    
    }
    
    //+号按钮点击事件
    func composBtnClick() {
        
        print("++++++++")
        
    }
    
    //添加子控制器
    private func addChildViewControllers() {
        
        //1.获取json文件路径
        let path = Bundle.main.path(forResource: "MainVCSettings", ofType: "json")
        
        //2.通过文件路径创建NSData
        if let jsonPath = path {
            
            let jsonData:NSData! = NSData.init(contentsOfFile: jsonPath)
            //3.序列化json数据
            do {
                //有可能会发生异常的代码
                //try:发生异常会跳转到catch中继续执行
                //try！发生异常直接崩溃
                let dicArr = try JSONSerialization.jsonObject(with: jsonData! as Data , options: JSONSerialization.ReadingOptions.mutableContainers)
               // print(dicArr)
                //在swift中，如果遍历一个数组，必须明确数据的类型
                for dict in dicArr as! [[String:String]]  {
                    addChildViewController(childControllerName: dict["vcName"]!, title: dict["title"]!, imageName: dict["imageName"]!)
                }
            } catch {
                
                //发生异常之后会执行的代码
                print("error")
                //从本地创建控制器
                addChildViewController(childControllerName: "HomeTableViewController", title: "首页", imageName: "tabbar_home")
                addChildViewController(childControllerName: "MessageTableViewController", title: "消息", imageName: "tabbar_message_center")
                addChildViewController(childControllerName: "NullViewController", title: "", imageName: "")
                addChildViewController(childControllerName: "DiscoverTableViewController", title: "广场", imageName: "tabbar_discover")
                addChildViewController(childControllerName: "ProfileTableViewController", title: "我", imageName: "tabbar_profile")
            }
            
        }

        
    }
    
    
    /// 初始化自控制器
    ///
    /// - Parameters:
    ///   - childController: 子控制器
    ///   - title: 子控制器标题
    ///   - imageName: 子控制图片
    private func addChildViewController(childControllerName: String, title: String, imageName:String) {
        
        
        let ns = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
        
        let cls:AnyClass? = NSClassFromString(ns + "." + childControllerName)
        
        let vcCls = cls as! UIViewController.Type
        
        let vc = vcCls.init()
        
       // print(vc)
        //1.1设置首页tabbar对应的数据
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        vc.tabBarItem.title = title
        
        //1.2设置导航条
        vc.navigationItem.title = title
        
        //2.给首页包装一个导航控制器
        let nav = UINavigationController()
        nav.addChildViewController(vc)
        
       // 3.将导航控制器添加到当前控制器
        addChildViewController(nav)
    }
    
    private lazy var composBtn: UIButton = {
        
        let btn = UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), for: .normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: .normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), for: .highlighted)

        btn.addTarget(self, action: #selector(MainViewController.composBtnClick), for: .touchUpInside)
        
        return btn
    }()
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
